﻿#region Header

/*
    This file is part of nDoctor Light

    NDoctor is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    NDoctor is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with NDoctor.  If not, see <http://www.gnu.org/licenses/>.
*/

#endregion Header

namespace Probel.Ldoc.ViewModels.Controls.Corectors
{
    using System;
    using System.Collections.ObjectModel;

    using Probel.Ldoc.Services.Dto;
    using Probel.Ldoc.Services.Services;
    using Probel.Ldoc.ViewModels.Pages;
    using Probel.Ldoc.ViewModels.Properties;
    using Probel.Mvvm.DataBinding;
    using Probel.Mvvm.Gui;

    public class InsuranceSpellingCorectorViewModel : BaseViewModel
    {
        #region Fields

        private readonly IncoherencesViewModel Parent;
        private readonly IPatientService Service;

        private IErrorService errorService;
        private Pages.IncoherencesViewModel incoherencesViewModel;
        private IPatientService patientService;
        private InsuranceCardDto selectedFix;
        private InsuranceCardDto selectedItem;

        #endregion Fields

        #region Constructors

        public InsuranceSpellingCorectorViewModel(IncoherencesViewModel parent, IErrorService errorService, IPatientService service, bool isAsync = true)
            : base(errorService, isAsync)
        {
            this.Service = service;
            this.Parent = parent;
            this.Parent.CanFix = false;

            this.ItemsToFix = new ObservableCollection<InsuranceCardDto>();
            this.SubItems = new ObservableCollection<InsuranceDto>();
        }

        #endregion Constructors

        #region Properties

        public ObservableCollection<InsuranceCardDto> ItemsToFix
        {
            get;
            private set;
        }

        public InsuranceCardDto SelectedFix
        {
            get { return this.selectedFix; }
            set
            {
                this.Parent.CanFix = !(value == null);
                this.selectedFix = value;
                this.OnPropertyChanged(() => SelectedFix);
            }
        }

        public InsuranceCardDto SelectedItem
        {
            get { return this.selectedItem; }
            set
            {
                this.selectedItem = value;
                this.GetSubItems(value);
                this.OnPropertyChanged(() => SelectedItem);
            }
        }

        public ObservableCollection<InsuranceDto> SubItems
        {
            get;
            private set;
        }

        #endregion Properties

        #region Methods

        public void ClearLists()
        {
            this.ItemsToFix.Clear();
            this.SubItems.Clear();
        }

        public void Fix()
        {
            this.Service.FixSpellingInsurances(this.SelectedFix);
        }

        private void GetSubItems(InsuranceCardDto value)
        {
            if (value != null)
            {
                var t = this.Service.GetInsurancesDoubloons(value.Name);
                this.SubItems.Refill(t);
            }
        }

        #endregion Methods
    }
}